package us.icitap.commandcenter.ui.supervisor;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.Action.Handler;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.AbstractSelect.ItemDescriptionGenerator;
import com.vaadin.ui.Window;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.CallActionEnum;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.CallStatusEnum;
import us.icitap.commandcenter.ui.custom.CustColumnGenerator;
import us.icitap.commandcenter.ui.custom.CustTable;
import us.icitap.commandcenter.ui.custom.CustVerticalLayout;
import us.icitap.commandcenter.ui.shared.CallInfoWindow;
import us.icitap.commandcenter.ui.shared.CallTable;
import us.icitap.dao.Expression;
import us.icitap.dao.Operator;
import us.icitap.utils.Result;

public class CallList extends CustVerticalLayout {

	private us.icitap.commandcenter.ui.shared.CallTable table;
	
	private final String lRedText = "TELEFONATA TE REJA: " ;
	private final String lYellowText = "NE PRITJE PER TU VERIFIKUAR: " ;
	private final String lGreenText = "TELEFONATA TE VERIFKUARA: ";
	private final String lGreenUNText = "NUMRA UNIK: ";
	
	
	private final Label lRed = new Label(lRedText + 0);
	private final Label lYellow = new Label(lYellowText + 0);
	private final Label lGreen = new Label(lGreenText + 0);
	private final Label lGreenUN = new Label(lGreenUNText + 0);
	
	private Integer redCount = 0;
	private Integer yellowCount = 0;
	private Integer greenCount = 0;
	private Long greenUNCount = 0l;
	
	
	private List<Call> callList = Collections.synchronizedList(new ArrayList<Call>()); 
	
	
	
	public CallList() {
		table = new CallTable("Totali i telefonatave ne sallen operative: ");
		 
		table.addGeneratedColumn("relatedCallNr", CustColumnGenerator.relatedCallNumberColumnGenerator);
		
		
		table.setVisibleColumns(new Object[]{"relatedCallNr","caller", "address", "description", "callReason", "mainDispatcher","recordDate", "userByStatus", "uniqueNumber"});
		table.setColumnHeaders(new String[]{"NR.TEL","TELEFONUESI", "ADRESA", "PERSHKRIMI", "ARSYEJA TEL.", "Z.MBULIMIT", "DT.REGJ.", "PERDORUESI", "NR.UNIK"});
		table.addGeneratedColumn("caller", CustColumnGenerator.callerColumnGenerator);
		table.addGeneratedColumn("callReason", CustColumnGenerator.callReasonColumnGenerator);
		table.addGeneratedColumn("mainDispatcher", CustColumnGenerator.dispatcherColumnGenerator);
		table.addGeneratedColumn("uniqueNumber", CustColumnGenerator.uniqueNumberColumnGenerator);
		table.setPageLength(10);
		table.setWidth("980px");
		
		table.setColumnWidth("relatedCallNr", 30);
		table.setColumnWidth("caller", 100);
		table.setColumnWidth("address", 150);
		table.setColumnWidth("description", 200);
		table.setColumnWidth("mainDispatcher", 150);
		table.setColumnWidth("uniqueNumber", 100);
		table.setImmediate(true);
		
		table.setCellStyleGenerator(new Table.CellStyleGenerator() {
			
			@Override
			public String getStyle(Table source, Object itemId,
					Object propertyId) {
				if(propertyId == null){
					if(((Call)itemId).getActualStatus().equals(CallStatusEnum.N))
						return "red";
					else if(((Call)itemId).getActualStatus().equals(CallStatusEnum.WV))
						return "yellow";
					else if(((Call)itemId).getActualStatus().equals(CallStatusEnum.V))
						return "green";
				}
				
				return null;
			}
		});
		

		this.updateLabels();
		
		table.setSelectable(true);
		this.addComponent(this.getTableHeader());
		this.addComponent(table);
		this.setSizeUndefined();
		
		
		table.setImmediate(true);
		
		this.initTable();
		
	}
	
	public void initTable(){

		Result<List<Call>> result = CommandCenterUI.appStatelessBean.getInitialNewCallList(CommandCenterUI.getUser().getCommandCenter());
	
		if(result.isSucceeded()){
			callList.addAll(result.getObject());
			redCount = result.getObject().size();
		}
		else
			CommandCenterUI.LOG.logp(Level.SEVERE,"SupervisorVerticalLayout","initTable()", result.getMessage());
		
		
		result =  CommandCenterUI.appStatelessBean.getWvCallList(CommandCenterUI.getUser().getCommandCenter().getActiveDispatcherList());
	
		if(result.isSucceeded()){
			callList.addAll(result.getObject());
			yellowCount = result.getObject().size();
		}
		else
			CommandCenterUI.LOG.logp(Level.SEVERE,"SupervisorVerticalLayout","initTable()", result.getMessage());
		
		

		result = CommandCenterUI.appStatelessBean.getClosedCallList(CommandCenterUI.getUser().getCommandCenter().getActiveDispatcherList());
		
		if(result.isSucceeded()){
			callList.addAll(result.getObject());
			greenCount = result.getObject().size();
		}
		else
			CommandCenterUI.LOG.logp(Level.SEVERE,"SupervisorVerticalLayout","initTable()", result.getMessage());
		
	
		table.addCallList(callList);
		Result<Long> resultUN =  CommandCenterUI.appStatelessBean.getCallPersistBean().findUniqueNumberTotal(CommandCenterUI.getUser().getCommandCenter());
		if(resultUN.isSucceeded()){
			greenUNCount =resultUN.getObject();
			
		}
		this.updateTable();
	}
	
	
	
	
	public void processCall(Call call, CallActionEnum callActionEnum) {
		
		if(CallActionEnum.NEW_CALL.equals(callActionEnum)){
			//Telefonate e re
			table.addCall(call);
			redCount += 1;
			updateTable();
			return;
		}
		
		this.table.removeItem(call);
		table.addCall(call);
		

		if(CallActionEnum.START_VERIFICATION.equals(callActionEnum)){
			redCount -= 1;
			yellowCount += 1;
		}
		
		else if(CallActionEnum.CLOSE_VERIFICATION.equals(callActionEnum)){
			yellowCount -= 1;
			greenCount += 1;
			if(call.getUniqueNumber() != null)
				greenUNCount += 1;
			
		}
		
		else if(CallActionEnum.GENERATE_UN.equals(callActionEnum)){
			greenUNCount += 1;
		}
		
		updateTable();
	}

	private void updateTable(){
		boolean[] order = {true,false};
		table.sort(new Object[]{"actualStatus", "recordDate"}, order);
		updateLabels();
		
	}
	
	public void updateLabels(){
		this.lRed.setValue(this.lRedText + this.redCount);
		this.lYellow.setValue(this.lYellowText + this.yellowCount);
		this.lGreen.setValue(lGreenText + this.greenCount);
		this.lGreenUN.setValue(lGreenUNText + this.greenUNCount);
	}
	
private HorizontalLayout getTableHeader(){
		
		lRed.addStyleName("boldLabel");
		lYellow.addStyleName("boldLabel");
		lGreen.addStyleName("boldLabel");
		lGreenUN.addStyleName("boldLabel");
		
		final HorizontalLayout hl = new HorizontalLayout();
		
		final VerticalLayout vlRed = new VerticalLayout();
		vlRed.setWidth("15px");vlRed.setHeight("15px");
		vlRed.addStyleName("red");
		
		final VerticalLayout vlYellow = new VerticalLayout();
		vlYellow.setWidth("15px");vlYellow.setHeight("15px");
		vlYellow.addStyleName("yellow");
		
		final VerticalLayout vlGreen = new VerticalLayout();
		vlGreen.setWidth("15px");vlGreen.setHeight("15px");
		vlGreen.addStyleName("green");
		
		final VerticalLayout vlGreenUN = new VerticalLayout();
		vlGreenUN.setWidth("15px");vlGreenUN.setHeight("15px");
		vlGreenUN.addStyleName("green");
		
		final HorizontalLayout hlRed = new HorizontalLayout();
		
		hlRed.addComponent(vlRed);
		hlRed.addComponent(lRed);
		hl.addComponent(hlRed);
		
		final HorizontalLayout hlYellow = new HorizontalLayout();
		hlYellow.addComponent(vlYellow);
		hlYellow.addComponent(lYellow);
		hl.addComponent(hlYellow);
		
		final HorizontalLayout hlGreen = new HorizontalLayout();
		hlGreen.addComponent(vlGreen);
		hlGreen.addComponent(lGreen);
		hl.addComponent(hlGreen);
		
		final HorizontalLayout hlGreenUN = new HorizontalLayout();
		hlGreenUN.addComponent(vlGreenUN);
		hlGreenUN.addComponent(lGreenUN);
		hl.addComponent(hlGreenUN);
		
		hl.setSpacing(true);
		hl.setSizeFull();
		hl.setMargin(true);
		return hl;
		
	}

	public void addActionHandler(Handler actionHandler){
		this.table.addActionHandler(actionHandler);
	}

	public CallTable getTable() {
		return table;
	}
	
	

}
